package service

import (
	global "firstdemo_go/global"
	"firstdemo_go/model"
)

func AddPerson(data model.Data) error {
	err := global.GVA_MYSQL.Create(data)
	return err.Error
}

func DeletePerson(id int64) error {
	var model model.Person
	result := global.GVA_MYSQL.Delete(&model, id)
	return result.Error
}

func UpdatePerson(data model.Person, id int64) error {
	var model model.Person
	row := global.GVA_MYSQL.First(&model, id)
	if row.Error == nil {
		result := global.GVA_MYSQL.Model(&model).Updates(&data)
		return result.Error
	}
	return row.Error
}

func FindPerson(id int64) (model.Person, error) {
	var model model.Person
	result := global.GVA_MYSQL.First(&model, id)
	return model, result.Error
}

func ListPerson(datas []model.Person, page int) ([]model.Person, int64, error) {
	var pageSize = 2
	offset := (page - 1) * pageSize
	result := global.GVA_MYSQL.Order("id desc").Offset(offset).Limit(pageSize).Find(&datas)
	return datas, result.RowsAffected, result.Error
}
